/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.utils;

import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetManager;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import exterminator.Main;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import tonegod.gui.core.utils.XMLHelper;
import tonegod.gui.style.StyleLoader;

/**
 *
 * @author t0neg0d
 */
public class LevelLoader {
	Main main;
	Map<String, SceneInfo> scenes = new LinkedHashMap();
	SceneInfo currentScene;
	public String expDir = "X";
	
	public LevelLoader(Main main) {
		this.main = main;
		main.getAssetManager().registerLoader(StyleLoader.class, "xml");
	}
	
	public void loadExpansions() {
		List<String> zipFiles = new ArrayList();

		File rootFile = new File("t.zip");
		String rootPath = rootFile.getAbsolutePath().substring(
			0,
			rootFile.getAbsolutePath().lastIndexOf(File.separator)+1
		);
		File dir = new File(rootPath);
		
		expDir = rootFile.getAbsolutePath();
		
		for (String file : dir.list()) {
			if (file.indexOf(".zip") != -1) {
				File tFile = new File(file);
				String tPath = tFile.getPath();
				zipFiles.add(tPath);
			}
		}

		AssetManager a;

		if (!zipFiles.isEmpty()) {
			int index = 1;
			for (String zip : zipFiles) {
				main.getAssetManager().registerLocator(zip, ZipLocator.class);
				String filename = zip.substring(0,zip.lastIndexOf("."));
				filename += "_config.xml";
				parseScenes(filename, zip);
				System.out.println("Parsed " + index + " expansion pack(s): " + zip);
				index++;
			}
		}
	}
	
	public void loadScenes(String filePath, String zip)
			{
		parseScenes(filePath, zip);
	}
	
	public void selectScene(String key) {
		currentScene = scenes.get(key);
	}
	
	public int getLevelCount() {
		return this.currentScene.levels.size();
	}
	
	public SceneInfo getCurrentScene() {
		return this.currentScene;
	}
	
	private void parseScenes(String path, String zip) {
		try {
			Document doc = main.getAssetManager().loadAsset(new AssetKey<Document>(path));
			Node topNode = doc.getElementsByTagName("scenes").item(0);
			NodeList nLst = ((Element)topNode).getElementsByTagName("scene");
			
			for (int s = 0; s < nLst.getLength(); s++) {
				Node fstNode = nLst.item(s);
				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
					SceneInfo sceneInfo = new SceneInfo();
					
					Element fstElmnt = (Element) fstNode;
					
					sceneInfo.key = getStringValue(fstElmnt, "key");
					sceneInfo.name = getStringValue(fstElmnt, "name");
					sceneInfo.dir = getStringValue(fstElmnt, "dir");
					sceneInfo.zip = zip;
					sceneInfo.time = getFloatValue(fstElmnt, "time");
					sceneInfo.winTotal = getIntegerValue(fstElmnt, "wintotal");
					sceneInfo.maxBugs = getIntegerValue(fstElmnt, "maxbugs");
					
					Element camAngle = (Element) fstElmnt.getElementsByTagName("camangle").item(0);
					sceneInfo.angles[0] = getFloatValue(camAngle, "x");
					sceneInfo.angles[1] = getFloatValue(camAngle, "y");
					sceneInfo.angles[2] = getFloatValue(camAngle, "z");
					
					sceneInfo.model = getStringValue(fstElmnt, "model");
					sceneInfo.diffuseImg = getStringValue(fstElmnt, "diffuse");
					
					NodeList nLstLvl = fstElmnt.getElementsByTagName("level");
					
					for (int l = 0; l < nLstLvl.getLength(); l++) {
						Node fstLvlNode = nLstLvl.item(l);
						if (fstLvlNode.getNodeType() == Node.ELEMENT_NODE) {
							LevelInfo levelInfo = new LevelInfo();

							levelInfo.index = l;

							Element fstLvlElmnt = (Element) fstLvlNode;

							Element camPos = (Element) fstLvlElmnt.getElementsByTagName("campos").item(0);
							levelInfo.camPosition.set(
								getFloatValue(camPos, "x"),
								getFloatValue(camPos, "y"),
								getFloatValue(camPos, "z")
							);

							levelInfo.model = getStringValue(fstLvlElmnt, "model");

							Element size = (Element) fstLvlElmnt.getElementsByTagName("size").item(0);
							levelInfo.size.set(
								getFloatValue(size, "x"),
								getFloatValue(size, "y")
							);

							sceneInfo.levels.add(levelInfo);
						}
					}
					scenes.put(sceneInfo.key, sceneInfo);
				}
			}
			
			doc = null;
		} catch (Exception e) {
			System.out.println("Problem loading scene: " + e);
		}
	}
	private String getStringValue(Element parent, String tag) {
		return XMLHelper.getNodeAttributeValue(parent.getElementsByTagName(tag).item(0), "value");
	}
	private float getFloatValue(Element parent, String tag) {
		return Float.parseFloat(XMLHelper.getNodeAttributeValue(parent.getElementsByTagName(tag).item(0), "value"));
	}
	private int getIntegerValue(Element parent, String tag) {
		return Integer.parseInt(XMLHelper.getNodeAttributeValue(parent.getElementsByTagName(tag).item(0), "value"));
	}
	
	public class SceneInfo {
		public String key;
		public String name;
		public String dir;
		public String zip;
		public float time;
		public int winTotal;
		public int maxBugs;
		
		public float[] angles = new float[3];
		
		public String model;
		public String diffuseImg;
		
		public List<LevelInfo> levels = new ArrayList();
	}
	
	public class LevelInfo {
		public int index;
		public Vector3f camPosition = new Vector3f();
		public String model;
		public Vector2f size = new Vector2f();
	}
}
